package edu.gatech.cs2340.teamrocket.control.guicontrol.networking;

import java.io.IOException;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
import edu.gatech.cs2340.teamrocket.control.networking.exception.TimeoutException;
import edu.gatech.cs2340.teamrocket.control.networking.impl.VTNetworkingInterface;
import edu.gatech.cs2340.teamrocket.control.networking.message.LoginFailureMessage;
import edu.gatech.cs2340.teamrocket.control.networking.message.LoginSuccessMessage;
import edu.gatech.cs2340.teamrocket.control.networking.message.Message;
import edu.gatech.cs2340.teamrocket.control.networking.message.ServerFullMessage;
import edu.gatech.cs2340.teamrocket.model.Model;
import edu.gatech.cs2340.teamrocket.view.networking.NetworkSetupGUIInterface;

/**
 *
 * @author Joe Tacheron
 */
public class NetworkSetupControl implements NetworkSetupControlInterface {

    private static final String IP_PATTERN   = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
    private static final String USER_PATTERN = "\\w[\\w ]{0,19}";
    private static final String PASS_PATTERN = "\\w{1,20}";

    private NetworkSetupGUIInterface gui;

    private String ip;
    private String user;
    private String pass;

    private Runnable onSuccess;

    public NetworkSetupControl(Runnable onSuccess){
    	this.onSuccess = onSuccess;
    }

    @Override
    public void setGUI(NetworkSetupGUIInterface gui) {
        this.gui = gui;
    }

    @Override
    public synchronized void startClientClicked() {
        try {
            ip   = gui.getIPAddress();
            user = gui.getClientUsername();
            pass = gui.getClientPassword();

            // validation
            if(!isValidIP(ip)){
                gui.errIPAddress("You enetred an invalid IP address");
                return;
            }
            if(!isValidUsername(user)){
                gui.errClientUsername("You enetred an invalid username");
                return;
            }
            if(!isValidPassword(pass)){
                gui.errClientPassword("You enetred an invalid password");
                return;
            }

            Message m = VTNetworkingInterface.getInstance().joinServer(ip, user, pass);

            if(m instanceof LoginSuccessMessage){
                JOptionPane.showMessageDialog(null, "You successfully logged in!");
                gui.close();
                Model.getInstance().setIP(ip);
                Model.getInstance().setUsername(user);
                Model.getInstance().setPassword(pass);

                new Thread(onSuccess).start();
            }
            else if(m instanceof LoginFailureMessage){
                JOptionPane.showMessageDialog(null, "The password you entered was incorrect.");
            }
            else if(m instanceof ServerFullMessage){
                JOptionPane.showMessageDialog(null, "The server is full.");
            }
            else{
                JOptionPane.showMessageDialog(null, "The server responded with gibberish! Sorry!");
            }
        }
        catch (UnknownHostException e) {
            JOptionPane.showMessageDialog(null, "What are you? Stupid? How am I supposed to interpret the host you gave me? Is it an IP address?");
            return;
        }
        catch (IOException e) {
            JOptionPane.showMessageDialog(null, "An IO error occurred and the host could not be reached.");
            return;
        }
        catch (TimeoutException e) {
            JOptionPane.showMessageDialog(null, "The server did not repsond in time.");
            return;
        }
    }

    @Override
    public synchronized void startServerClicked() {
        try {
            ip   = VTNetworkingInterface.getInstance().getIPAddress();
            user = gui.getServerUsername();
            pass = gui.getServerPassword();

            // validation
            if(!isValidUsername(user)){
                gui.errServerUsername("You enetred an invalid username");
                return;
            }
            if(!isValidPassword(pass)){
                gui.errServerPassword("You enetred an invalid password");
                return;
            }

            VTNetworkingInterface.getInstance().startServer(user, pass);

            gui.close();
            Model.getInstance().setIP(ip);
            Model.getInstance().setUsername(user);
            Model.getInstance().setPassword(pass);

            new Thread(onSuccess).start();
        }
        catch (UnknownHostException e) {
            JOptionPane.showMessageDialog(null, "What are you? Stupid? How am I supposed to interpret the host you gave me? Is it an IP address?");
            return;
        }
        catch (IOException e) {
            JOptionPane.showMessageDialog(null, "An IO error occurred and the host could not be reached.");
            return;
        }
        catch (TimeoutException e) {
            JOptionPane.showMessageDialog(null, "The server did not repsond in time.");
            return;
        }
    }

    @Override
    public synchronized void cancelClicked(){
        gui.close();
    }

    private boolean isValidIP(String ip){
        return ip.matches(IP_PATTERN);
    }

    private boolean isValidUsername(String user){
        return user.matches(USER_PATTERN);
    }

    private boolean isValidPassword(String pass){
        return pass.matches(PASS_PATTERN);
    }
}